﻿using System;

namespace TimHeuer.WindowsLiveWriter.S34Writer.S3Model
{
    public class S3Object
    {
        private const string isoDateFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";

        public S3Object()
        {
        }

        public S3Object(S3Bucket bucket, string key, string storageClass,
            string size, string owner, string lastModified)
        {
            _bucket = bucket;
            _key = key;
            _storageClass = storageClass;
            _size = size;
            _owner = owner;
            ParseAndSetLastModified(lastModified);
        }

        public S3Object(ListBucketResultContents contents)
        {
            if (contents == null)
            {
                return;
            }

            _key = contents.Key;
            _storageClass = contents.StorageClass;
            _size = contents.Size;
            if ((contents.Owner != null) && (contents.Owner.Length > 0))
            {
                _owner = contents.Owner[0].DisplayName;
            }
            ParseAndSetLastModified(contents.LastModified);
        }

        private S3Bucket _bucket;

        public S3Bucket Bucket
        {
            get { return _bucket; }
            set { _bucket = value; }
        }

        private string _key;

        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }

        private string _storageClass;

        public string StorageClass
        {
            get { return _storageClass; }
            set { _storageClass = value; }
        }

        private string _size;

        public string Size
        {
            get { return _size; }
            set { _size = value; }
        }

        private string _owner;

        public string Owner
        {
            get { return _owner; }
            set { _owner = value; }
        }

        private DateTime _lastModified;

        public DateTime LastModified
        {
            get { return _lastModified; }
            set { _lastModified = value; }
        }

        public void ParseAndSetLastModified(string lastModified)
        {
            if (!DateTime.TryParseExact(lastModified, isoDateFormat, System.Globalization.CultureInfo.InvariantCulture,
                System.Globalization.DateTimeStyles.None, out _lastModified))
            {
                _lastModified = DateTime.Now;
            }
        }
    }
}
